
import path = require("path")
import fs = require("fs")
import express = require("express");
import { Config } from "../src/config/Config";
import { Logger } from "../src/engine/utils/Logger";
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser')
const http = require("http");
const https = require("https");

/**
 * https服务器 cj33.cn
 */
export class Https_Server {

    public static instance: Https_Server = new Https_Server()


    public async main() {

        let port: number = parseInt(process.argv[2]);
        if (isNaN(port)) {
            Logger.warn("port为空")
            return;
        }
        let environment: number = parseInt(process.argv[3]);
        if (isNaN(port)) {
            Logger.warn("environment为空")
            return;
        }
        Config.environment = environment;
        if (Config.environment == Config.EnvironmentType.Online) {
            // Logger.LEVEL = Logger.LOG_LEVEL_TYPES.WARN;
            Logger.LEVEL = Logger.LOG_LEVEL_TYPES.DEBUG
        } else {
            Logger.LEVEL = Logger.LOG_LEVEL_TYPES.DEBUG
        }
        Config.serverType = Config.ServerType_List.Https_Server;
        Logger.tag = Config.serverType + "-" + port

        let keyBasePath = path.resolve() + "/ssl"
        Logger.log("keyBasePath====", keyBasePath);
        let keyPath:string = "/9967563_cj33.cn.key"
        let pempath:string = "/9967563_cj33.cn.pem"
        // if (Config.environment == Config.EnvironmentType.xj) {
        //     keyPath = "/xj22.in.key"
        //     pempath = "/xj22.in.pem"
        // }
        const httpsOption = {
            key: fs.readFileSync(keyBasePath + keyPath),
            cert: fs.readFileSync(keyBasePath + pempath)
        }


        // Logger.log("config=", configObj[""])

        let app = express();
        
        let publicPath = path.resolve() + "/public";
        // if (Config.environment == Config.EnvironmentType.jhao_fenfa) {
        //     if (!fs.existsSync(publicPath)) {
        //         fs.mkdirSync(publicPath);
        //     }
        // }
        // else if(Config.environment == Config.EnvironmentType.xj){
        //     publicPath = path.resolve() + "/public"+port;
        //     if (!fs.existsSync(publicPath)) {
        //         fs.mkdirSync(publicPath);
        //     }
        // }else{
            publicPath = path.resolve() + "/public"+port;
            if (!fs.existsSync(publicPath)) {
                fs.mkdirSync(publicPath);
            }
        // }
        app.use(bodyParser.urlencoded({ extended: false })); //解析post请求数据
        app.use(bodyParser.text()); // for parsing application/json
        app.use(bodyParser.json()); // for parsing application/json

        //设置静态网页目录
        let options = {
            setHeaders: function (res, path, stat) {
                res.set('Access-Control-Allow-Origin', '*')
            }
        }
        app.use(express.static(publicPath, options));
        app.use(cookieParser())
        app.all('*', function (req, res, next) {
            req.header("Content-Type", "application/json;charset=UTF-8");
            res.header("Access-Control-Allow-Origin", "*");//项目上线后改成页面的地址
            res.header("Content-Type", "application/json;charset=UTF-8");
            res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
            // if (Config.environment == Config.EnvironmentType.jhao_fenfa) {
            //     next();
            // } else {
                // res.redirect('https://down.33eb.cn/l4yMBV');

                // if(Config.environment == Config.EnvironmentType.xj){
                //     // res.redirect('https://down.33eb.cn/fnd1m3');
                //     next();
                // }else{
                    next();
                    // res.redirect('https://down.33eb.cn/H9Sqku');
                // }
            // }
        });
        https.createServer(httpsOption, app).listen(port);

        let app2 = express();
        app2.use(bodyParser.urlencoded({ extended: false })); //解析post请求数据
        app2.use(bodyParser.text()); // for parsing application/json
        app2.use(bodyParser.json()); // for parsing application/json
        app2.all('*', function (req, res, next) {
            req.header("Content-Type", "application/json;charset=UTF-8");
            res.header("Access-Control-Allow-Origin", "*");//项目上线后改成页面的地址
            res.header("Content-Type", "application/json;charset=UTF-8");
            res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
            // if (Config.environment == Config.EnvironmentType.jhao_fenfa) {
            //     next();
            // } else {
                // if(Config.environment == Config.EnvironmentType.xj){
                //     // res.redirect('https://down.33eb.cn/fnd1m3');
                //     res.redirect('https://xj22.in');
                // }else{
                    res.redirect('https://cj33.cn');
                    // res.redirect('https://down.33eb.cn/H9Sqku');
                // }
             
            // }
        });
        http.createServer(app2).listen(80);
        Logger.info("start suc=", port);

    }
}

try {
    Https_Server.instance.main()

} catch (error) {
}